package com.seveneight.baijiesteward.ui.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseFragmentBack;
import com.seveneight.baijiesteward.bean.CreditBean;
import com.seveneight.baijiesteward.bean.OrderBean;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.AppConfig;
import com.seveneight.baijiesteward.ui.main.contract.LoginContract;
import com.seveneight.baijiesteward.ui.main.presenter.LoginPresenter;
import com.seveneight.baijiesteward.ui.user.activity.CreditCardListActivity;
import com.seveneight.baijiesteward.ui.user.contract.CreateOderContract;
import com.seveneight.baijiesteward.ui.user.presenter.CreateOrderPresenter;
import com.seveneight.baijiesteward.ui.user.activity.RecordActivity;
import com.seveneight.baijiesteward.ui.user.contract.CreditCardListContract;
import com.seveneight.baijiesteward.ui.user.presenter.CreditCardListPresenter;
import com.seveneight.baijiesteward.utils.StringFormat;
import com.seveneight.baijiesteward.utils.ToastUtil;
import com.seveneight.baijiesteward.utils.WindowUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by user1 on 2018/3/28.
 */

public class PayBackFragment extends BaseFragmentBack implements View.OnClickListener, CreateOderContract.CreateOderView, CreditCardListContract.CreditCardListView, LoginContract.LoginView {

    LoginContract.LoginPresenter loginPresenter;
    CreateOderContract.CreateOderPresenter createOderPresenter;
    CreditCardListContract.CreditCardListPresenter creditCardListPresenter;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_wBalance;
    private TextView tv_wBalance1;
    private TextView tv_wAbleBalance;
    private TextView tv_wUnAbleBalance;
    private TextView bt_submit;
    private TextView tv_right;
    private TextView tv_cardInfo;
    private TextView tv_moreCard;
    private EditText et_inputMoney;
    private EditText et_tracePwd;
    private CreditBean creditBean;
    private UserBean userBean;

    public static PayBackFragment newInstance() {
        Bundle args = new Bundle();
        PayBackFragment fragment = new PayBackFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_payback;
    }

    @Override
    protected void initView() {
        tv_title = findView(R.id.tv_title);
        tv_right = findView(R.id.tv_right);
        tv_title.setText("还款金");
        tv_right.setText("还款记录");
        tv_wBalance1 = findView(R.id.tv_wBalance);
        tv_wAbleBalance = findView(R.id.tv_wAbleBalance);
        tv_wUnAbleBalance = findView(R.id.tv_wUnAbleBalance);
        bt_submit = findView(R.id.bt_submit);
        tv_cardInfo = findView(R.id.tv_cardInfo);
        tv_moreCard = findView(R.id.tv_moreCard);
        et_inputMoney = findView(R.id.et_inputMoney);
        et_inputMoney.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        et_tracePwd = findView(R.id.et_tracePwd);
        iv_back = findView(R.id.iv_back);
    }

    @Override
    protected void initData() {
        loginPresenter = new LoginPresenter(this);
        createOderPresenter = new CreateOrderPresenter(this);
        creditCardListPresenter = new CreditCardListPresenter(this);
        creditCardListPresenter.getCreditCardList(mContext, jsonFormat.commonParam());
        loginPresenter.getUserInfo("正在更新余额信息...",mContext, jsonFormat.commonParam());
    }

    @Override
    protected void setListener() {
        tv_right.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        tv_moreCard.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                _mActivity.onBackPressed();
                break;
            case R.id.tv_moreCard:
                Intent i = new Intent(mContext, CreditCardListActivity.class);
                startActivityForResult(i, AppConfig.REQUEST_CODE_0);
                break;
            case R.id.bt_submit:
                submit();
                break;
            case R.id.tv_right:
                openRecord();
                break;
        }
    }

    private void openRecord() {
        Bundle bundle1 = new Bundle();
        bundle1.putString("type", "1");
        WindowUtil.getInstance().startActivity(mContext, RecordActivity.class, bundle1);
    }

    private void submit() {
        String wABalance = userBean.getAbleBalance();//可用的还款金
        String newBalance = et_inputMoney.getText().toString().trim();//还款的金额
        String trancePwd = et_tracePwd.getText().toString();
        if (wABalance.equals("0.0000")) {
            showDialog("暂无可用的还款金");
            return;
        }
        if (TextUtils.isEmpty(newBalance)) {
            showDialog("请输入使用金额");
            return;
        }
        double money = Double.parseDouble(wABalance);
        double minMoney = Double.parseDouble(newBalance);
        BigDecimal data1 = new BigDecimal(money);
        BigDecimal data2 = new BigDecimal(minMoney);
        if (data2.compareTo(data1) == 1) {
            showDialog("超出可用的还款金");
            return;
        }
        if (creditBean == null) {
            showDialog("请选择还款卡账号");
            return;
        }
        if (TextUtils.isEmpty(trancePwd)) {
            showDialog("请输入交易密码");
            return;
        }
        createOderPresenter.createOrder(mContext, jsonFormat.createOrder(newBalance,
                "1",
                creditBean.getCreditCardID(),
                null,
                null,
                trancePwd, 1));
    }

    private void initBottom(CreditBean creditBean) {
        tv_cardInfo.setText(creditBean.getBankName() + "(" + creditBean.getCreditCardNo().substring(creditBean.getCreditCardNo().length() - 4) + ")");
        tv_moreCard.setText("更换");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == AppConfig.REQUEST_CODE_0 && resultCode == RESULT_OK) {
            creditBean = (CreditBean) data.getSerializableExtra("bean");
            initBottom(creditBean);
        }
    }

    @Override
    public void reBackOrderInfo(OrderBean orderBean) {
        showDialog("提交成功");
        openRecord();
    }

    @Override
    public void reBackCreditCardList(List<CreditBean> data) {
        if (data.size() < 1) {
            tv_cardInfo.setText("暂时没有信用卡");
            tv_moreCard.setVisibility(View.GONE);
        } else {
            creditBean = data.get(0);
            tv_moreCard.setVisibility(View.VISIBLE);
            initBottom(creditBean);
        }
    }

    @Override
    public void reBackkUser(UserBean bean) {

    }

    @Override
    public void backUserInfo(UserBean bean) {
        setting.setUserInfo(bean);
        userBean = bean;
        tv_wBalance1.setText(StringFormat.formatNumber(bean.getBalance()));
        tv_wAbleBalance.setText(StringFormat.formatNumber(bean.getAbleBalance()));
        tv_wUnAbleBalance.setText(StringFormat.formatNumber(bean.getUnAbleBalance()));
    }
}
